package com.example.guiguiapplicatio.fragment.Systemsettings;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.guiguiapplicatio.MainActivity;
import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.fragment.FamilySigning.FamilySigingNextActivity;
import com.example.guiguiapplicatio.util.ResponseBody;
import com.example.guiguiapplicatio.utils.ApiUtils;
import com.example.guiguiapplicatio.utils.PreferencesUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import cn.refactor.lib.colordialog.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangePasswordFrament extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ImageButton servic_back;
    private EditText tiwen_edit1;
    private EditText tiwen_edit2;
    private EditText tiwen_edit3;
    private Button confirm_the_contract;
    private ApiUtils apiUtils;

    public ChangePasswordFrament() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PoorFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ChangePasswordFrament newInstance(String param1, String param2) {
        ChangePasswordFrament fragment = new ChangePasswordFrament();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        BottomNavigationView bottomNavigationView=requireActivity().findViewById(R.id.nav_view);
//        bottomNavigationView.setVisibility(View.INVISIBLE);
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_user_system_settings_revise, container, false);
        servic_back = root.findViewById(R.id.servic_back);
        tiwen_edit1 = root.findViewById(R.id.tiwen_edit1);
        tiwen_edit2 = root.findViewById(R.id.tiwen_edit2);
        tiwen_edit3 = root.findViewById(R.id.tiwen_edit3);
        confirm_the_contract = root.findViewById(R.id.confirm_the_contract);
        apiUtils = new ApiUtils(getString(R.string.api));
        confirm_the_contract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!tiwen_edit2.getText().toString().equals(tiwen_edit3.getText().toString())){
                    new PromptDialog(getContext())
                            .setDialogType(PromptDialog.DIALOG_TYPE_INFO)
                            .setAnimationEnable(true)
                            .setTitleText("INFO")
                            .setContentText("两次新密码不一致")
                            .setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
                                @Override
                                public void onClick(PromptDialog dialog) {
                                    dialog.dismiss();
                                }
                            }).show();
                }
                else {
                    Call<ResponseBody<Object>> responseBodyCall =  apiUtils.getApi().ChangePassword(tiwen_edit2.getText().toString(),tiwen_edit1.getText().toString(), PreferencesUtil.getInt(getContext(),"doctor_id"));
                    responseBodyCall.clone().enqueue(new Callback<ResponseBody<Object>>() {
                        @Override
                        public void onResponse(Call<ResponseBody<Object>> call, Response<ResponseBody<Object>> response) {
                            ResponseBody<Object> objectResponseBody = response.body();
                            if(objectResponseBody != null && objectResponseBody.getCode() == 200){
                                new PromptDialog(getContext())
                                        .setDialogType(PromptDialog.DIALOG_TYPE_SUCCESS)
                                        .setAnimationEnable(true)
                                        .setTitleText("SUCCESS")
                                        .setContentText("密码修改成功，请重新登录")
                                        .setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
                                            @Override
                                            public void onClick(PromptDialog dialog) {
                                                dialog.dismiss();
                                            }
                                        }).show();
                            }
                            else {
                                new PromptDialog(getContext())
                                        .setDialogType(PromptDialog.DIALOG_TYPE_WRONG)
                                        .setAnimationEnable(true)
                                        .setTitleText("WRONG")
                                        .setContentText("旧密码错误，请重新输入")
                                        .setPositiveListener("OK", new PromptDialog.OnPositiveListener() {
                                            @Override
                                            public void onClick(PromptDialog dialog) {
                                                dialog.dismiss();
                                            }
                                        }).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody<Object>> call, Throwable t) {

                        }
                    });
                }
            }
        });

        servic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.action_ChangePasswordFrament_to_SystemsettingsFragment);
            }
        });
        return root;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView=requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }
}
